const fs = require('fs');
const path = require('path');

// 检查项目中的渠道筛选实现
function checkChannelFilter() {
  console.log('🔍 检查项目中的渠道筛选实现...\n');
  
  const srcDir = path.join(process.cwd(), 'src');
  const files = findVueFiles(srcDir);
  
  console.log(`📁 找到 ${files.length} 个Vue文件\n`);
  
  const results = [];
  
  for (const file of files) {
    const result = analyzeFile(file);
    if (result) {
      results.push(result);
    }
  }
  
  // 分类显示结果
  const needsUpdate = results.filter(r => r.needsUpdate);
  const alreadyUpdated = results.filter(r => r.isUpdated);
  
  console.log('📊 检查结果汇总:');
  console.log(`  总文件数: ${results.length}`);
  console.log(`  需要更新: ${needsUpdate.length}`);
  console.log(`  已更新完成: ${alreadyUpdated.length}\n`);
  
  if (needsUpdate.length > 0) {
    console.log('🚨 需要更新的文件:');
    needsUpdate.forEach(result => {
      console.log(`  ❌ ${result.file}`);
      console.log(`      - 问题: ${result.issues.join(', ')}`);
    });
    console.log('');
  }
  
  if (alreadyUpdated.length > 0) {
    console.log('✅ 已更新完成的文件:');
    alreadyUpdated.forEach(result => {
      console.log(`  ✅ ${result.file}`);
    });
    console.log('');
  }
  
  console.log('🎯 检查完成！');
}

// 递归查找Vue文件
function findVueFiles(dir, files = []) {
  const items = fs.readdirSync(dir);
  
  for (const item of items) {
    const fullPath = path.join(dir, item);
    const stat = fs.statSync(fullPath);
    
    if (stat.isDirectory()) {
      if (!['node_modules', '.git', 'dist', 'build'].includes(item)) {
        findVueFiles(fullPath, files);
      }
    } else if (path.extname(item) === '.vue') {
      files.push(fullPath);
    }
  }
  
  return files;
}

// 分析文件内容
function analyzeFile(filePath) {
  try {
    const content = fs.readFileSync(filePath, 'utf8');
    const relativePath = path.relative(process.cwd(), filePath);
    
    // 检查是否包含渠道相关关键词
    const hasChannel = content.toLowerCase().includes('channel');
    
    if (!hasChannel) {
      return null;
    }
    
    // 检查各种实现方式
    const hasInput = content.includes('el-input') && content.includes('channel');
    const hasSelect = content.includes('el-select') && content.includes('channelOptions');
    const hasChannelMixin = content.includes('channelMixin');
    const hasFormatChannelParam = content.includes('formatChannelParam');
    
    const issues = [];
    let needsUpdate = false;
    let isUpdated = false;
    
    if (hasInput && !hasSelect) {
      issues.push('使用输入框，需要改为下拉选择');
      needsUpdate = true;
    }
    
    if (!hasChannelMixin) {
      issues.push('未使用channelMixin');
      needsUpdate = true;
    }
    
    if (!hasFormatChannelParam) {
      issues.push('未使用formatChannelParam');
      needsUpdate = true;
    }
    
    if (hasSelect && hasChannelMixin && hasFormatChannelParam) {
      isUpdated = true;
    }
    
    return {
      file: relativePath,
      hasInput,
      hasSelect,
      hasChannelMixin,
      hasFormatChannelParam,
      needsUpdate,
      isUpdated,
      issues
    };
  } catch (error) {
    console.error(`Error reading file ${filePath}:`, error.message);
    return null;
  }
}

// 运行检查
checkChannelFilter(); 